package com.gegong.imessage.utils;

import org.dom4j.*;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;

import java.io.*;
import java.util.List;
import java.util.Map;

public class MacConfigUtils {


    public static  class  ConfigReplaceScheme{
        private String key;


    }

//    public static File replaceWuma(){
//
//    }


    public static File createConfigFileFromTemplate(File templateConfigFile, Map<String, String> replaces, File dest) throws DocumentException, IOException {
        SAXReader reader = new SAXReader(); //1.创建一个xml解析器对象

        Document doc = reader.read(templateConfigFile);//2.读取xml文档，返回Document对象

        //Element rootElement = doc.getRootElement();  //获取xml文档的根标签（一般创建doc对象后回先调用此方法得到根标签）

        //用xpath找到所有的key标签

        List nodes = doc.selectNodes("//key[text()='IDE']");
//        List nodes = doc.selectNodes("//key[deep-equal(text(),'IDE')]");
        nodes.forEach(o -> {
            Element e = (Element) o;
            //e.setText("asdfadfafd");
//            System.out.println(e.getText());
        });
        System.out.println(nodes.size());


        FileOutputStream out = new FileOutputStream(dest);//指定文件输出的位置
        //指定写出的格式
        OutputFormat format = OutputFormat.createCompactFormat(); //紧凑的格式.去除空格换行.
        //OutputFormat format = OutputFormat.createPrettyPrint(); //格式好的格式.有空格和换行.
        format.setEncoding("utf-8");//2.指定生成的xml文档的编码
        XMLWriter writer = new XMLWriter(out, format);//创建写出对象
        writer.write(doc);//写出对象
        writer.close();//关闭流
        // elem.
        return dest;
    }


    public static void main(String[] args) throws DocumentException, IOException {

//        System.out.println( );


        File templateFile = new File(MacConfigUtils.class.getResource("/config.plist").getFile());
        System.out.println(templateFile.exists());
        MacConfigUtils.createConfigFileFromTemplate(templateFile,null,
                null);
    }
}
